//
//  MyTeamUserView.swift
//  LeGou
//
//  Created by lxm on 2022/7/30.
//

import UIKit

class MyTeamUserTreeView : UIView {

    //MARK: --------------------属性--------------------
    
    //MARK: --------------------UI控件--------------------
    let me = ZMUtil.imageView(imageName: "def_user_img")
    let vip = ZMUtil.imageView(imageName: "team_vip")

    let subarray = [ItemView(), ItemView(), ItemView(), ItemView(), ItemView()]
    let topLine = [UIView(), UIView(), UIView(), UIView(), UIView()]
    let acrossLine = UIView()
    let verticalLine = UIView()
    //MARK: --------------------生命周期--------------------
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
        bindAction()
        addConstraints()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setUI()
        bindAction()
        addConstraints()
    }

    //MARK: --------------------UI--------------------
    func setUI() {
        addSubview(me)
        me.zm.setCenterX(zm.width / 2)
        me.zm.setY(24)
        me.zm.setSize(CGSize(width: 44, height: 44))
        me.layer.cornerRadius = 22
        me.clipsToBounds = true
        
        addSubview(vip)

        
        let count = subarray.count
        let width = zm.width / CGFloat(count)
        let height : CGFloat = 65
        let y = zm.height - 24 - height
        let lineheight : CGFloat = 22
        let lineColor = "#E6E6E6".zm.hex()
        for i in 0 ..< count {
            let v = subarray[i]
            let line = topLine[i]
            addSubview(line)
            addSubview(v)
            v.frame = CGRect(x: CGFloat(i) * width, y:  y, width: width, height: height)
            line.backgroundColor = lineColor
            line.frame = CGRect(x: width / 2 + CGFloat(i) * width, y: y - lineheight, width: 1, height: lineheight)
        }
        // 画横线
        addSubview(acrossLine)
        acrossLine.backgroundColor = lineColor
        acrossLine.frame = CGRect(x: width / 2, y: y - lineheight, width: width * (CGFloat(count - 1)), height: 1)
        
        // 画竖线
        addSubview(verticalLine)
        verticalLine.backgroundColor = lineColor
        verticalLine.frame = CGRect(x: zm.width / 2, y: me.zm.bottom, width: 1, height: y - lineheight - me.zm.bottom)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setUI()
    }
    
    func addConstraints() {
        vip.snp.makeConstraints { make in
            make.right.bottom.equalTo(me)
        }
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
        
    }
    //MARK: --------------------delegate--------------------

    
}

extension MyTeamUserTreeView {
    class ItemView : UIView {
        let avatar = ZMUtil.imageView(imageName: "def_user_img")

        
        let name = ZMUtil.label(title: "10282.87282.87", color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 12, weight: .regular)).then({$0.textAlignment = .center})
        
        let vip = ZMUtil.imageView(imageName: "team_vip")
        
        override init(frame: CGRect) {
            super.init(frame: frame)
            setUI()
        }
        
        required init?(coder: NSCoder) {
            super.init(coder: coder)
            setUI()
        }
        
        func setUI() {
            addSubview(avatar)
            addSubview(name)
            addSubview(vip)
            avatar.layer.cornerRadius = 20
            avatar.clipsToBounds = true

            avatar.snp.makeConstraints { make in
                make.width.height.equalTo(40)
                make.centerX.equalToSuperview()
                make.top.equalToSuperview()
            }
            
            name.snp.makeConstraints { make in
                make.top.equalTo(avatar.snp.bottom).offset(4)
                make.left.right.equalToSuperview()
            }
            
            vip.snp.makeConstraints { make in
                make.right.bottom.equalTo(avatar)
            }
        }
    }
}

